//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.net.mugui.net.pc.task;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import com.mugui.base.client.net.bagsend.NetCall;

import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;

public class WSUtil {
	private static HashMap<String, NetCall> wsNetCalls = new HashMap();

	public WSUtil() {
	}

	public static void ws(String ws_server, HashMap<String, String> sessionhead, TMWsHandle wshandle) {
		if (wshandle.getSocket() == null) {
			Class var4 = WSUtil.class;
			synchronized (WSUtil.class) {
				if (wshandle.getSocket() == null) {
					OkHttpClient okHttpClient = (new Builder()).readTimeout(10L, TimeUnit.SECONDS)
							.writeTimeout(10L, TimeUnit.SECONDS).connectTimeout(10L, TimeUnit.SECONDS).build();
					okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
					Set<Entry<String, String>> entrySet = sessionhead.entrySet();
					Iterator<Entry<String, String>> var8 = entrySet.iterator();

					while (var8.hasNext()) {
						Entry<String, String> entry = var8.next();
						if (StringUtils.isNotBlank((CharSequence) entry.getKey())
								&& StringUtils.isNotBlank((CharSequence) entry.getValue())) {
							builder.addHeader((String) entry.getKey(), (String) entry.getValue());
						}
					}

					okHttpClient.newWebSocket(builder.url(ws_server).build(), wshandle);
					okHttpClient.dispatcher().executorService().shutdown();
				}
			}
		}


	}

	public static NetCall getWsNetCall(String func) {
		return (NetCall) wsNetCalls.get(func);
	}

	public static NetCall subNetCall(String func) {
		NetCall netCall = null;
		synchronized (wsNetCalls) {
			netCall = (NetCall) wsNetCalls.get(func);
			if (netCall == null) {
				netCall = new NetCall();
				wsNetCalls.put(func, netCall);
			}
		}

		return (NetCall) wsNetCalls.get(func);
	}

	public static void unsubNetCall(String func) {
		synchronized (wsNetCalls) {
			wsNetCalls.remove(func);
		}
	}
}
